﻿using Padsurface.App.Services.Interfaces;
using System;
using System.Composition;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Padsurface.App.Services
{
    [Export(typeof(INavigationService))]
    internal class NavigationService : INavigationService
    {
        public bool Navigate(Type sourcePageType)
        {
            return RootFrame().Navigate(sourcePageType);
        }

        public bool Navigate(Type sourcePageType, object parameter)
        {
            return RootFrame().Navigate(sourcePageType, parameter);
        }

        public bool GoBack()
        {
            if (!RootFrame().CanGoBack)
                return false;

            RootFrame().GoBack();
            return true;
        }

        private Frame RootFrame()
        {
            var rootFrame = Window.Current.Content as Frame;
            if (rootFrame != null)
                return rootFrame;

            rootFrame = new Frame();

            Window.Current.Content = rootFrame;
            Window.Current.Activate();

            return rootFrame;
        }
    }
}